package com.example.kugou.controller;


import com.example.kugou.common.fileUpload.FileUploadTool;
import com.example.kugou.common.music.FileUtils;
import com.example.kugou.common.music.Mp3Info;
import com.example.kugou.pojo.Mv;
import com.example.kugou.pojo.Sing;
import com.example.kugou.pojo.Singer;
import com.example.kugou.service.MvService;
import com.example.kugou.service.SingService;
import com.example.kugou.service.SingerService;
import com.example.kugou.vo.BangdanVO;
import com.example.kugou.vo.SingVO;
import com.example.kugou.vo.SreachVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sing")
@Api(tags = "歌曲相关接口",description = "提供歌曲相关的Rest API")
public class SingController{
    @Resource
    SingService singService;


    @Resource
    SingerService singerService;

    @Resource
    MvService mvService;

    public SingService getSingService() {
        return singService;
    }

    public void setSingService(SingService singService) {
        this.singService = singService;
    }

    /**
     * 根据id查询歌曲,并播放
     */
    @ApiOperation("根据id查询歌曲,并播放")
    @GetMapping("/selectById")
    public Map<String,Object> selectById(int id) {
        Map<String,Object> map=new HashMap<>();
        Sing sing = singService.selectById(id);
        map.put("sing",sing);
        return map;
    }

    /**
     * 查询所有歌曲
     */
    @ApiOperation("查询歌曲")
    @GetMapping("/SelectSing")
    public Map<String,Object> SelectSing() {
        Map<String,Object> map=new HashMap<>();
        List<Sing> sings = singService.SelectSing();
        map.put("sings",sings);
        System.out.println(sings);
        return map;
    }
    /**
     * 查询所有歌曲
     */
    @ApiOperation("查询所有歌曲")
    @GetMapping("/selectAllSing")
    public SingVO SelectAllSing(@RequestParam(value = "SingName",required = false,defaultValue = "")String SingName, @RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex) {
      SingVO singVO = new SingVO();
        PageHelper.startPage(pageIndex,20);
        List<Sing> sings = singService.SelectAllSing(SingName);
        PageInfo<Sing> pageInfo = new PageInfo<Sing>(sings);
        singVO.setPageInfo(pageInfo);
        singVO.setSings(sings);
        return singVO;
    }


    @ApiOperation("根据歌单查询歌单中的歌曲")
    @GetMapping("/selectSingByAId")
    public Map<String, Object> selectSingByAId(int ssid) {
        Map<String, Object> map = new HashMap<>();
        List<Sing> sings = singService.selectBysong_sheetid(ssid);
        map.put("sings", sings);
        return map;
    }


    @ApiOperation("根据时间排序歌曲")
    @GetMapping("/selectSingsOrderByTime")
    public BangdanVO selectSingsOrderByTime() {
        BangdanVO bangdanVO = new BangdanVO("",false);
        List<Sing> sings = new ArrayList<>();
        try {
            sings = singService.selectSingsOrderByTime();
            bangdanVO = new BangdanVO("",true,sings,"xinge");
        } catch (Exception e) {
            bangdanVO = new BangdanVO(e.getMessage(),false);

        }
        System.out.println(bangdanVO);
        return bangdanVO;
    }

    @ApiOperation("根据Id排序歌曲")
    @GetMapping("/selectOrderById")
    public BangdanVO selectOrderById() {
        BangdanVO bangdanVO = new BangdanVO("",false);
        List<Sing> sings = new ArrayList<>();
        try {
            sings = singService.selectOrderById();
            bangdanVO = new BangdanVO("",true,sings,"biaosheng");
        } catch (Exception e) {
            bangdanVO = new BangdanVO(e.getMessage(),false);

        }
        return bangdanVO;
    }


    @ApiOperation("我的主页听歌排行")
    @GetMapping("/selectPaiHang")
    public SingVO PaiHang(){
        SingVO singVO = new SingVO();
        List<Sing> sings = singService.selectPaiHang();
        if(sings.size()>0){
            singVO.setMsg("查询排行成功");
            singVO.setStatus(true);
            singVO.setSings(sings);
        }else{
            singVO.setMsg("查询排行失败");
            singVO.setStatus(false);
        }
        return singVO;
    }


    @ApiOperation("根据编号删除歌曲")
    @RequestMapping("/removeBySing")
    public SingVO delete(@RequestParam(value = "nid",required = false)Integer nid){
         SingVO singVO = new SingVO();
         int i = singService.deleteBySing(nid);
         if(i>0){
             singVO.setMsg("删除成功");
             singVO.setStatus(true);
         }else{
             singVO.setMsg("删除失败");
             singVO.setStatus(false);
         }
               return singVO;
    }


    @ApiOperation("添加歌曲")
    @PostMapping("/insertSing")
    public void insertSing(MultipartFile inputfile, MultipartFile inputsrcfile, String introduce,
                           @RequestParam(value = "typeId",defaultValue = "0")int typeId, HttpServletRequest request){
        Mp3Info mp3Info =  new FileUploadTool().toMusic(inputfile,inputsrcfile,request);
        Sing sing = new Sing();
        Singer singer = null;
        sing.setMusic(mp3Info);
        sing.setType(typeId);
        sing.setIntroduce(introduce);
        singer = singerService.selectByName(mp3Info.getSingerName());
        if (singer != null && singer.getRid() > 0){
            sing.setSingerId(singer.getRid());
        }else {
            int count = singerService.insertSinger(new Singer(mp3Info.getSingerName()));
            if (count > 0){
                singer = singerService.selectByName(mp3Info.getSingerName());
                if(singer != null && singer.getRid() > 0){
                    sing.setSingerId(singer.getRid());
                }
            }else {
                sing.setSingerId(1);
            }
        }
        singService.insertSing(sing);
    }




    @ApiOperation("根据名字查询")
    @GetMapping("/sreach")
    public SreachVO sreach(String sreachInfo,int type){
        SreachVO sreachVO = new SreachVO("查询失败",false);
        switch (type){
            case 1:
                //查询歌曲
                List<Sing> sings = singService.selectName(sreachInfo);
                sreachVO.setSreachSings(sings);
                sreachVO.setMsg("查询成功");
                sreachVO.setStatus(true);
                System.out.println(sings);
                break;
            case 2:
                //查询歌手
                List<Singer> singers =  singerService.selectName(sreachInfo);
                sreachVO.setSreachSingers(singers);
                sreachVO.setMsg("查询成功");
                sreachVO.setStatus(true);
                System.out.println(singers);
                break;
            case 3:
                //专辑

                break;
            case 4:
                //视频mv
                List<Mv> mvs = mvService.selectName(sreachInfo);
                sreachVO.setSreachMv(mvs);
                sreachVO.setMsg("查询成功");
                sreachVO.setStatus(true);
                System.out.println(mvs);
                break;
            case 5:
                //歌词
                    Map<String,List<String>> map = FileUtils.getGeci(new File("G:\\小组项目\\网易云\\网易云\\163mHome\\lrc"),sreachInfo);
                    sreachVO.setMsg("查询成功");
                    sreachVO.setStatus(true);
                    System.out.println(map);
                break;
            case 6:
                //歌单

                break;
            case 7:
//                主播电台
                break;
            case 8:
                //用户

                break;
                default:
                    break;
        }
        return sreachVO;
    }
}

